//
//  XGVedioTopicView.m
//  BSBuDeJie
//
//  Created by monkey on 2019/1/1.
//  Copyright © 2019 itcast. All rights reserved.
//

#import <Masonry/Masonry.h>

#import "UIImageView+Extension.h"

#import "XGVideoTopicView.h"

#import "XGTopicModel.h"

@interface XGVideoTopicView ()

/// 图片视图
@property (nonatomic,strong) UIImageView *imageView;
/// 播放按钮
@property (nonatomic,strong) UIButton *playButton;
/// 播放数量
@property (nonatomic,strong) UILabel *playCountLabel;
/// 播放时长
@property (nonatomic,strong) UILabel *playDurationLabel;

@end

@implementation XGVideoTopicView

#pragma mark - 解析模型

- (void)setTopicModel:(XGTopicModel *)topicModel
{
    _topicModel = topicModel;
    
    // 设置图片
    [self.imageView xg_setImageWithURLString:topicModel.imageURL placeHolder:kPlaceHolderImage];
    // 设置播放次数
    self.playCountLabel.text = [[NSString stringFromNumberToString:topicModel.playcount] stringByAppendingString:@"次播放"];
    
    // 设置时长
    self.playDurationLabel.text = [NSString stringWithFormat:@"%02zd:%02zd",topicModel.videotime / 60,topicModel.videotime % 60];
}

#pragma mark - 构造方法

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI
{
    // 添加子控件
    [self addSubview:self.imageView];
    [self addSubview:self.playButton];
    [self addSubview:self.playCountLabel];
    [self addSubview:self.playDurationLabel];
    
    // 设置自动布局
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [self.playCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self);
    }];
    
    [self.playDurationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self);
    }];
}

#pragma mark - 懒加载

- (UIImageView *)imageView
{
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds = YES;
    }
    
    return _imageView;
}

- (UIButton *)playButton
{
    if (_playButton == nil) {
        _playButton = [UIButton buttonWithImageName:@"video-play" highlightedImageName:@"video-play" target:nil action:nil];
    }
    
    return _playButton;
}

- (UILabel *)playCountLabel
{
    if (_playCountLabel == nil) {
        _playCountLabel = [UILabel labelWithText:@"测试文本" textColor:[UIColor whiteColor] font:15 textAlignment:NSTextAlignmentCenter];
        _playCountLabel.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
    
    return _playCountLabel;
}

- (UILabel *)playDurationLabel
{
    if (_playDurationLabel == nil) {
        _playDurationLabel = [UILabel labelWithText:@"测试文本" textColor:[UIColor whiteColor] font:15 textAlignment:NSTextAlignmentCenter];
        _playDurationLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
    
    return _playDurationLabel;
}

@end
